package com.lwei.ljpro.controller;

import com.lwei.ljpro.dto.Message;
import com.lwei.ljpro.emun.CookieName;
import com.lwei.ljpro.emun.ProcessResult;
import com.lwei.ljpro.entry.UserEntry;
import com.lwei.ljpro.service.IUserService;
import com.lwei.ljpro.util.CookieUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/loginCenterRest")
public class LoginCenterController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/check")
    public Message check(String userTel, String userName, HttpServletResponse response){

        //创建用户
        Long result = userService.saveUser(new UserEntry(userTel, userName));

        if(result != null){
            CookieUtils.writeCookie(response, "/", CookieName.USER_TEL.getValue(), userTel);
            CookieUtils.writeCookie(response, "/", CookieName.USER_NAME.getValue(), userName);
            return Message.instance(ProcessResult.SUCCESS);
        }else{
            return Message.instance(ProcessResult.FAIL);
        }

    }
}
